package pl.register.android.activities.correlation.ui;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import pl.register.android.R;
import pl.register.android.classes.ClassesTopic;
import pl.register.android.homework.HomeworkTopic;
import pl.register.android.lesson.LessonProgram;
import pl.register.android.program.Program;
import pl.register.android.widget.textview.VerticalTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Mateusz Strzelecki
 * @since 2013-05-07
 */
public class CorrelationHolidayLessonHeadersAdapter {

    private View.OnClickListener onClickListener;

    private View.OnLongClickListener onLongClickListener;

    private LayoutInflater inflater;

    private LinearLayout llClassesTopics;

    private LinearLayout llClassesSchedule;

    private Program program;


    public CorrelationHolidayLessonHeadersAdapter(View correlationHeaders, LayoutInflater inflater, View.OnClickListener onClickListener, View.OnLongClickListener onLongClickListener) {
        this.inflater = inflater;
        this.onClickListener = onClickListener;
        this.onLongClickListener = onLongClickListener;

        program = new Program(new ArrayList<LessonProgram>());
        llClassesTopics = (LinearLayout) correlationHeaders.findViewById(R.id.holiday_classes_topics_linear_layout);
        llClassesSchedule = (LinearLayout) correlationHeaders.findViewById(R.id.holiday_classes_schedule_linear_layout);

    }

    private void clearHeaders() {
        llClassesTopics.removeAllViews();
        llClassesSchedule.removeAllViews();
    }

    private void build() {
        clearHeaders();
        int index = 1;
        for (LessonProgram lessonProgram : program.getLessons()) {
            boolean isLast = (index == program.getLessons().size());
            llClassesTopics.addView(getClassesTopicView(lessonProgram.getClassesTopics()));
            llClassesSchedule.addView(getClassesScheduleView(lessonProgram, isLast));
            index++;
        }
    }

    private View getClassesTopicView(List<ClassesTopic> classesTopics) {
        View view = null;
        if (classesTopics.size() == 1) {
            view = inflater.inflate(R.layout.table_cell_vertical_topic, null);
            VerticalTextView vtvTopic = (VerticalTextView) view.findViewById(R.id.topic_vertical_text_view);
            vtvTopic.setText(classesTopics.get(0).getTopic());
        } else if (classesTopics.size() == 2) {
            view = inflater.inflate(R.layout.table_cell_vertical_double_topic, null);
            VerticalTextView vtvTopic1 = (VerticalTextView) view.findViewById(R.id.topic_1_vertical_text_view);
            vtvTopic1.setText(classesTopics.get(0).getTopic());
            VerticalTextView vtvTopic2 = (VerticalTextView) view.findViewById(R.id.topic_2_vertical_text_view);
            vtvTopic2.setText(classesTopics.get(1).getTopic());
        } else {
            Toast.makeText(inflater.getContext(), "Ilość zajęć nie obsługiwana.", Toast.LENGTH_SHORT).show();
        }
        return view;
    }

    private View getClassesScheduleView(LessonProgram lessonProgram, boolean isLast) {
        View view = inflater.inflate(R.layout.table_cell_classes_schedule, null);
        Button btnSchedule = (Button) view.findViewById(R.id.classes_schedule_button);
        btnSchedule.setText(lessonProgram.getClassesDateInString());
        btnSchedule.setTag(lessonProgram);
        btnSchedule.setOnClickListener(onClickListener);
        if (isLast) {
            btnSchedule.setOnLongClickListener(onLongClickListener);
        }
        return view;
    }

    public void refresh(Program program) {
        this.program = program;
        build();
    }

}
